package com.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ydd.constant.JwtClaimsConstant;
import com.ydd.constant.MessageConstant;
import com.ydd.constant.StatusConstant;
import com.ydd.dto.FrontRegDTO;
import com.ydd.dto.SysUserDTO;
import com.ydd.dto.UserListDTO;
import com.ydd.dto.YddUserDTO;
import com.ydd.entity.YddUser;
import com.ydd.exception.AccountLockedException;
import com.ydd.exception.PasswordErrorException;
import com.ydd.mapper.UserMapper;
import com.ydd.properties.JwtProperties;
import com.ydd.service.UserService;
import com.ydd.utils.CommonUntil;
import com.ydd.utils.JwtUtil;
import com.ydd.vo.JWtToken;
import com.ydd.vo.RowInfoVO;
import com.ydd.vo.UserListRequestVO;
import com.ydd.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    JwtProperties jwtProperties;

    /**
     * 用户详情
     * @param id
     * @return
     */
    @Override
    public RowInfoVO rowInfo(Long id) {
        RowInfoVO rowInfoVO = userMapper.rowInfo(id);
        return rowInfoVO;
    }


    /**
     * 用户列表
     * @param userListRequestVO
     * @return
     */
    @Override
    public UserListVO userList(UserListRequestVO userListRequestVO) {
        PageHelper.startPage(Integer.parseInt(userListRequestVO.getPage()), Integer.parseInt(userListRequestVO.getLimit()));
        Page<YddUser> page = userMapper.pageQuery(userListRequestVO);
        long total = page.getTotal();
        List<YddUser> records = page.getResult();
        UserListVO userListVO= new UserListVO();
        userListVO.setCount(new Long (total).intValue());
        userListVO.setPageSize(Integer.parseInt(userListRequestVO.getLimit()));
        userListVO.setTotalPages(page.getPages());
        userListVO.setCurrentPage(Integer.parseInt(userListRequestVO.getPage()));
        //  List<RowInfoVO> data = new ArrayList<RowInfoVO>();

//        if(records != null){
//            for (int i = 0; i < records.size(); i++) {
//                YddUser abc = records.get(i);
//                RowInfoVO userItemVO = new RowInfoVO();

//                userItemVO.setAddress(abc.getAddress());
//                userItemVO.setBirthday(abc.getBirthday());
//                userItemVO.setIcon(abc.getIcon());
//                userItemVO.setId(abc.getId());
//                userItemVO.setUsername(abc.getUsername());
//                userItemVO.setMobile(abc.getMobile());
//                data.add(userItemVO);

//            }
//        }
//
        userListVO.setData(records);
        return userListVO;
    }

    /**
     * 用户添加
     * @param yddUserDTO
     *
     */
    @Override
    public void insert(YddUserDTO yddUserDTO) {
        YddUser yddUser = new YddUser();
        BeanUtils.copyProperties(yddUserDTO, yddUser);
        yddUser.setPassword(DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes()));
        yddUser.setSex(Integer.parseInt(yddUserDTO.getSex()));
        // 设置默认数据
        yddUser.setDefaultData(String.valueOf(1));

        // 调用UserMapper添加用户
        userMapper.insertOne(yddUser);

    }

    /**
     * 用户删除
     * @param id
     * @return
     */
    @Override
    public RowInfoVO del(Long id) {
        userMapper.del(id);
        return null;
    }


    /**
     * 用户修改
     * @param yddUser
     */
    @Override
    public void update(YddUser yddUser) {
        if(yddUser.getPassword() != null && !yddUser.equals("")){
            String md5DigestAsHex = DigestUtils.md5DigestAsHex(yddUser.getPassword().getBytes());
            yddUser.setPassword(md5DigestAsHex);
        }
        userMapper.update(yddUser);
    }

    /**
     * 用户登录
     *
     * @param loginDTO
     * @return
     */
    @Override
    public JWtToken login(FrontRegDTO loginDTO) {
        String token = null;
        LambdaQueryWrapper<YddUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(YddUser::getMobile, loginDTO.getMobile());
        YddUser user = userMapper.selectOne(lambdaQueryWrapper);

        // 账户密码判定
        if (user == null) {
            // 用户不存在
            throw new PasswordErrorException(MessageConstant.ACCOUNT_NOT_FOUND);
        } else {
            // 密码MD5转换
            if (DigestUtils.md5DigestAsHex(user.getPassword().getBytes()).equals(loginDTO.getPassword())) {
                throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
            } else {
                // 开始生成token
                token = CommonUntil.getToken(user.getId(), jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl());
            }
        }

        // 登录成功生成token

        return new JWtToken(token);
    }

    @Override
    public YddUser selectById(Integer id) {
        YddUser yddUser = userMapper.selectById(id);
        yddUser.setPassword("");
        return yddUser;
    }
}








